package com.ttl.bc.ap.invoice.action;



import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceBO;
import com.ttl.common.util.CommonUtils;

public class SelectBCAPInvoiceAction extends DispatchAction {		

	public ActionForward blank_summaryInv(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");												
						
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectBCAPInvoiceAction #mode :blank_summaryInv");
		
		return mapping.findForward("blank_summaryInv");
	}		
	
	@SuppressWarnings("unchecked")
	public ActionForward summaryInv(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectBCAPInvoiceAction #mode :summaryInv");
				
		new BCAPInvoiceBO().summaryInv(form, request);
		
		InvFindForm invForm =(InvFindForm)form;
		
		String docNoParam = " i.docNo in (";
		for(int i=0; i<invForm.getDocNoArray().length; i++){
			docNoParam+="'"+invForm.getDocNoArray()[i]+"',";
		}
		docNoParam = docNoParam.substring(0, docNoParam.length()-1  ) +")";
		//System.out.println(docNoParam);

		String nameOfReport= "/ireport/p2_summary.jasper";
		
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
		hashMap.put("slipNo", invForm.getSlipNo());
		hashMap.put("reportDate", CommonUtils.convertStringToDate(invForm.getReportDate()));

		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = getServlet().getServletConfig()
//							.getServletContext().getResourceAsStream(nameOfReport);
//		
//		response.setContentType("application/pdf");		
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
//
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();

		return null;
	}	
		
	
}
